'use strict';

const Service = require('egg').Service;
const uuid = require('uuid');
class AnnounceService extends Service {
  /**
   * 获取未审核公告列表
   */
  async getAnnounceListByApproval () {
    // 获取通告数据
    const list = await this.app.model.Announce.findAll({
      where: {
        status: 0,
      },
      include: [{
        model: this.app.model.User,
        attributes: ['realname'],
      }],
    }
      // {
      //   include: [{ // include关键字表示关联查询
      //     model: User, // 指定关联的model
      //     // 由于前面建立映射关系时为class表起了别名，那么这里也要与前面保持一致，否则会报错
      //     attributes: [ 'realname' ], // 这里的attributes属性表示查询class表的name和rank字段，其中对name字段起了别名className
      //   }],
      //   raw: true,
      // }
    );
    return list;
  }

  /**
   * 获取公告列表
   */
  async getAnnounceList () {
    // 获取通告数据
    // const list = await this.app.model.Announce.findAll({
    //   include: [{
    //     model: this.app.model.User,
    //     attributes: [ 'realname' ],
    //   }],
    // }
    // );
    const { QueryTypes } = require('sequelize');
    const list = await this.app.model.query(
      'SELECT an.*,u.realname from announce an, user u WHERE an.user_id=u.id',
      {
        type: QueryTypes.SELECT,
      });
    return list;
  }
  async getAnnounceListByQiYong () {
    const { QueryTypes } = require('sequelize');
    const list = await this.app.model.query(
      'SELECT an.*,u.realname from announce an, user u WHERE an.user_id=u.id and an.status = 1',
      {
        type: QueryTypes.SELECT,
      });
    return list;
  }
  // eslint-disable-next-line jsdoc/require-param
  /**
   * 通过城市编码获取公告列表
   */
  async getListByRegion (cityCode) {
    console.log('----------getListByRegion------------');
    console.log(cityCode);

    // 获取通告数据
    const { QueryTypes } = require('sequelize');
    const list = await this.app.model.query(
      'SELECT an.* from region r,announce an WHERE r.city_code=:cityCode and an.status="1" AND r.id=an.region_id',
      {
        replacements: { cityCode },
        type: QueryTypes.SELECT,
      }
    );
    return list;
  }
  // eslint-disable-next-line jsdoc/check-param-names
  /**
   * 保存公告
   * @param {公告}} announce
   */
  async saveAnnounce (announce) {
    announce.release_time = Date();
    console.log(uuid.v1());
    announce.announce_id = uuid.v1().replace(new RegExp('-', 'gm'), '');
    const announces = await this.app.model.Announce.create(announce);
    return announces;
  }
  async update (announce) {
    const announces = await this.app.model.Announce.update(announce, {
      where: {
        announce_id: announce.announce_id,
      },
    });
    return announces;
  }
  // eslint-disable-next-line jsdoc/require-param
  /**
   * 模糊查询公告，支持标题和状态查询
   */
  async selectAnnouncesByLike (params) {
    console.log('----------------------selectAnnouncesByLike-----------------------');
    const Op = this.app.Sequelize.Op; // 获取Sequelize的Op
    let title = '%%';
    let status = ['0', '1', '2'];
    // const status = '';
    // hasOwnProperty:判断参数是否含有这个key
    if (params.hasOwnProperty('title')) {
      title = '%' + params.title + '%';// 定义一个变量去接收
    }
    if (params.hasOwnProperty('status')) {
      status = [];
      status[0] = params.status;// 定义一个变量去接收
    }
    const announces = await this.app.model.Announce.findAll({
      where: {
        status: {
          [Op.in]: status,
        },
        title: {
          [Op.like]: title,
        },
      }, include: [{
        model: this.app.model.User,
        attributes: ['realname'],
      }]
    });
    return announces;
  }
  // eslint-disable-next-line jsdoc/require-param
  /**
   * 删除公告接口
   */
  async deleteAnnounce (announce_id) {
    console.log('-----------------------deleteAnnounce----------------------------');
    const announce = await this.app.model.Announce.destroy({ where: { announce_id } });
    return announce;
  }
  async getAnnounceDetail (announce_id) {
    console.log('-----------------------getAnnounceDetail----------------------------');
    const announce = await this.app.model.Announce.findOne({ where: { announce_id } });
    return announce;
  }
  async addLiked (announce_id) {
    console.log('-----------------------addLiked----------------------------');
    const { QueryTypes } = require('sequelize');
    const announce = await this.app.model.query(
      'UPDATE announce SET `like`=(`like`+1)  WHERE announce_id=:announce_id', {
      replacements: { announce_id },
      type: QueryTypes.UPDATE,
    });
    return announce;
  }
  async getAnnounceByUserSub (user_id) {
    console.log('----------getAnnounceByUserSub------------');
    console.log(user_id);
    // 获取通告数据
    const { QueryTypes } = require('sequelize');
    const list = await this.app.model.query(
      'SELECT an.* FROM announce an,user u,subscribe s WHERE u.id=s.user_id AND an.region_id=s.sub_id AND u.id=:user_id AND an.status="1" order by an.release_time desc',
      {
        replacements: { user_id },
        type: QueryTypes.SELECT,
      }
    );
    return list;
  }
  async approvingAnnounce (id, status, transaction) {
    console.log('-----------approvingAnnounce------------');
    console.log(status);
    console.log(id);
    return await this.app.model.Announce.update({
      status,
    }, {
      where: {
        announce_id: id,
      }, transaction,
    });
  }
}
module.exports = AnnounceService;
